package com.google.gwt.sample.stockwatcher.client;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.i18n.client.*;  
import com.google.gwt.core.client.EntryPoint;  
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;  
import com.google.gwt.user.client.ui.Widget;

import org.moxieapps.gwt.highcharts.client.*;  
import org.moxieapps.gwt.highcharts.client.labels.*;  
import org.moxieapps.gwt.highcharts.client.plotOptions.*;  
  
public class StockPriceChart implements StockPriceObserver{  
	private Chart chart;
	private Map<String,Series> stockPriceSeries = new HashMap<String,Series>();
	
	public StockPriceChart() {
		chart = createChart();
	}
	
    public Chart createChart() {  
  
        final Chart chart = new Chart()  
            .setType(Series.Type.SPLINE)  
            .setMarginRight(10)  
            .setChartTitleText("Progreso acciones bursatiles")  
            .setBarPlotOptions(new BarPlotOptions()  
                .setDataLabels(new DataLabels()  
                    .setEnabled(true)  
                )  
            )   
            .setLegend(new Legend()  
                .setLayout(Legend.Layout.VERTICAL)  
                .setAlign(Legend.Align.RIGHT)  
                .setVerticalAlign(Legend.VerticalAlign.TOP)  
                .setX(-10)  
                .setY(100)  
                .setBorderWidth(0)  
            )  
            .setCredits(new Credits()  
                .setEnabled(false)  
            )  
            .setToolTip(new ToolTip()  
                .setFormatter(new ToolTipFormatter() {  
                    public String format(ToolTipData toolTipData) {  
                        return "<b>" + toolTipData.getSeriesName() + "</b><br/>" +  
                            DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss")  
                                .format(new Date(toolTipData.getXAsLong())) + "<br/>" +  
                            NumberFormat.getFormat("0.00").format(toolTipData.getYAsDouble());  
                    }  
                })  
            ); 
        
        
        chart.getXAxis()  
            .setType(Axis.Type.DATE_TIME)  
            .setTickPixelInterval(150);  
  
        chart.getYAxis()  
            .setAxisTitleText("Value")  
            .setPlotLines(  
                chart.getYAxis().createPlotLine()  
                    .setValue(0)  
                    .setWidth(1)  
                    .setColor("#808080")  
            );  
            
        
        
        return chart;  
    }

	@Override
	public void onStockPriceAdded(StockPrice stockPrice) {
		
		final Series series = chart.createSeries(); 
		
		  // Generate an array of random data  
        long time = new Date().getTime();  
        for(int i = -19; i <= 0; i++) {  
            series.addPoint(time + i * 1000, com.google.gwt.user.client.Random.nextDouble());  
        } 
		
		series.addPoint(  
                new Date().getTime(),  
               stockPrice.getPrice(),
                true, true, true
            );  
		
        chart.addSeries(series  
            .setName(stockPrice.getSymbol())  
        );
        
        stockPriceSeries.put(stockPrice.getSymbol(), series);
	
	}

	@Override
	public void onStockPriceDeleted(StockPrice stockPrice) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStockPricesUpdated(HistoricoStockPrice historicoStockPrice, List<String> ids) {
				
		for (String id : ids) {
			List<StockPrice> listasp = historicoStockPrice.getHistory(id); 
			Series series = stockPriceSeries.get(id);
			
			series.addPoint(  
	                new Date().getTime(),  
	               listasp.get(listasp.size()-1).getPrice(),
	                false, true, true
	            );  			
			
		}
		
		chart.redraw();
		
		
		
	}

	@Override
	public Widget getWidget() {
		
		return chart;
	}  
  
}  